package edu.hawaii.myisern.model;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.util.List;
import edu.hawaii.myisern.collaborations.jaxb.Collaboration;
import edu.hawaii.myisern.organizations.jaxb.Organization;
import edu.hawaii.myisern.researchers.jaxb.Researcher;

/**
 * Demonstrates that the MyIsernModel is working.
 * 
 * @author Edward Kim
 * @author JianFei Liao
 * @author KaYee Leung
 */
public class TestMyIsernModel {
  /**
   * Tests the MyIsernModel.
   * 
   * @throws Exception If problems occur.
   */
  @Test
  public void testGetterMethods() throws Exception {
    MyIsernModel myIsernModel = MyIsernModel.getInstance();

    // Verifies methods for retrieving entire list of researchers, organizations, or collaborations.
    List<Researcher> researchers = myIsernModel.getResearchersList();
    assertEquals("Checking researchers size", 39, researchers.size());
    List<Collaboration> collaborations = myIsernModel.getCollaborationsList();
    assertEquals("Checking collaborations size", 19, collaborations.size());
    List<Organization> organizations = myIsernModel.getOrganizationsList();
    assertEquals("Checking organization size", 47, organizations.size());
 
    // verifies the save function
    myIsernModel.save();
    
    // verifies the addOrEdit methods for researcher
    Researcher newResearcher = new Researcher();
    newResearcher.setName("New Researcher");
    // add the New Researcher and save
    myIsernModel.addOrEditResearcher(newResearcher); 
    // get the researcher and verify the name
    Researcher researcher = myIsernModel.getSingleResearcher("New_Researcher");
    assertEquals("Checking researcher name", "New Researcher", researcher.getName());
    // edit the New Researcher
    researcher.setName("Old Reseasrcher");
    myIsernModel.addOrEditResearcher(researcher);
    // get the researcher again and verify the name
    researcher = myIsernModel.getSingleResearcher("Old_Reseasrcher");
    assertEquals("Checking researcher name", "Old Reseasrcher", researcher.getName());
    
    // verifies the addOrEdit methods for organization
    Organization newOrganization = new Organization();
    newOrganization.setName("New Organization");
    // add the New Organization
    myIsernModel.addOrEditOrganization(newOrganization);   
    // get the organization and verify the name
    Organization organization = myIsernModel.getSingleOrganization("New_Organization");
    assertEquals("Checking organization name", "New Organization", organization.getName());
    // edit the New Organization
    organization.setName("Old Organization");
    myIsernModel.addOrEditOrganization(organization);
    // get the organization again and verify the name
    organization = myIsernModel.getSingleOrganization("Old_Organization");
    assertEquals("Checking organization name", "Old Organization", organization.getName());
    
    // verifies the addOrEdit methods for collaboration
    Collaboration newCollaboration = new Collaboration();
    newCollaboration.setName("New Collaboration");
    // add the New Collaboration
    myIsernModel.addOrEditCollaboration(newCollaboration);   
    // get the collaboration and verify the name
    Collaboration collaboration = myIsernModel.getSingleCollaboration("New_Collaboration");
    assertEquals("Checking collaboration name", "New Collaboration", collaboration.getName());
    // edit the New Collaboration
    collaboration.setName("Old Collaboration");
    myIsernModel.addOrEditCollaboration(collaboration);
    // get the collaboration again and verify the name
    collaboration = myIsernModel.getSingleCollaboration("Old_Collaboration");
    assertEquals("Checking collaboration name", "Old Collaboration", collaboration.getName());
  }
}